using UnityEngine;
using System.Collections;

public class GUIControl : MonoBehaviour {
	
	public delegate void voidFunction();
	public delegate bool boolFunction();
	
	public GUITexture guiBtnAttack;
	public GUITexture guiBtnDefence;
	public GUITexture guiBtnJump;
	public GUITexture guiBtnMenu;
	
	public GameObject player = null;
	
	private voidFunction jumpFunction = null;
	private boolFunction attackFunction = null;
	
	// Use this for initialization
	void Start () {
		
		if(player == null)
		 	player = GameObject.FindGameObjectWithTag("Player");
		jumpFunction = player.GetComponent<Mover>().Jump;
		attackFunction = player.GetComponent<Attacker>().Attack;
		
	}
	
	// Update is called once per frame
	void Update () {
		
		Vector3 pointingPos = Vector3.zero;
		bool successHitTest = false;
		
		
		
		
		//Window or Mac Platform
		if(Application.platform != RuntimePlatform.IPhonePlayer)
		{
			if(Input.GetMouseButtonDown(0))
			{
				pointingPos = Input.mousePosition;
				successHitTest = onTouchOrMousePressd(pointingPos);
				//OK
				//print(pointingPos.ToString());
			}
			
		}else
		{
			foreach(Touch evt in Input.touches)
			{
				pointingPos = new Vector3(evt.position.x , evt.position.y,0);
				successHitTest = onTouchOrMousePressd(pointingPos);
				if(successHitTest)
					break;
			}
		}
	}
	
	protected bool onTouchOrMousePressd(Vector3 pos)
	{
		if(guiBtnAttack.HitTest(pos))
		{
			return onAttackPressed();
		}else if(guiBtnDefence.HitTest(pos))
		{
			return onDefencePressed();
		}else if(guiBtnJump.HitTest(pos))
		{
			return onJumpPressed();
		}else if(guiBtnMenu.HitTest(pos))
		{
			return onMenuPressed();
		}
		return false;
	}
	
	protected bool onAttackPressed()
	{
		attackFunction();
		print("onAttackPressed");
		
		return true;
	}
	protected bool onDefencePressed()
	{
		print("onDefencePressed");
		return true;
	}
	protected bool onJumpPressed()
	{
		jumpFunction();
		print("onJumpPressed");
		return true;
	}
	protected bool onMenuPressed()
	{
		print("onMenuPressed");
		return true;
	}
}
